// import api from '@/api/api'
// import user from './modules/user'
const state = () => ({
    // token: localStorage.getItem('token') || null,
    // // 确保 userInfo 为空时返回 null，不会抛出错误
    // userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null
})

const mutations = {
    // SET_TOKEN(state, token) {
    //     state.token = token
    //     localStorage.setItem('token', token)
    // },
    SET_USER_INFO(state, { userInfo, token }) {
        // console.log("haha" + userInfo);
        // console.log(userInfo);
        state.userInfo = userInfo
        state.token = token
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
        localStorage.setItem('token', token)
    }
    // ,
    // CLEAR_AUTH(state) {
    //     // state.token = null
    //     state.userInfo = null
    //     // localStorage.removeItem('token')
    //     localStorage.removeItem('userInfo')
    // }
}

const actions = {
    async setUserInfo({ commit }, { userInfo, token }) {
        // console.log(' 登录请求参数:', JSON.stringify(formData, null, 2));
        // console.log(formData + 'user 模块可能未正确注册！');

        commit('SET_USER_INFO', { userInfo, token })
            // return res
    },
    logout({ commit }) {
        commit('CLEAR_AUTH')
    }
}

const getters = {
    // isAuthenticated: state => !!state.token,
    currentUser: state => state.userInfo
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}